﻿using System;
using System.Data;
using System.Diagnostics;
using System.Web;
using DBScript;
using eBlog.Common;
using eBlog.Common.Files;
using eBlog.Service;
using eBlog.Entity;

namespace eBlog.Core
{

    /// <summary>
    /// basecode
    /// </summary>
    public class BaseCode
    {
        #region base
        /// <summary>
        /// 页面的执行时间
        /// </summary>
        public string ProcessTime
        {
            get
            {
                object value = HttpContext.Current.Items["ProcessTimer"];

                if (value != null)
                    return ((Stopwatch)value).Elapsed.TotalSeconds.ToString();

                return "0";
            }
        }

        /// <summary>
        /// 页面的数据库查询次数
        /// </summary>
        protected string QueryTimes
        {
            get
            {
                object value = HttpContext.Current.Items["ProcessTimer"];

                if (value != null)
                    return ((Stopwatch)value).Elapsed.TotalSeconds.ToString();

                return "0";
            }
        }

        /// <summary>
        /// 自动转换重写url
        /// </summary>
        /// <param name="dynUrl"></param>
        /// <returns></returns>
        public string Url(string dynUrl)
        {
            // return UrlRouter.Web.ReverseRoute.GetUrl(dynUrl);
            return dynUrl;
        }
        #endregion

        #region posts
        /// <summary>
        /// get author'name by userid
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string getAuthorbyID(int userId)
        {
          User user= UserService.GetUserById(userId);
          return user == null ? "Admin" : user.LoginName;

        }

        public string getAuthorLink(int? userId)
        {
            User user = null;

            if(userId.HasValue)
            {
                 user = UserService.GetUserById(userId.Value);
            }
           
            string author = "admin";
            string authorlink = "";
            if(user!=null)
            {
                authorlink = ConfigHelper.SiteUrl + "index.aspx?" + "author=" + user.ID;
                author = user.LoginName;

            }

            return string.Format("<a href=\"{0}\" title=\"作者:{1}\">{1}</a>", authorlink, author);
        }

        public string getMonthByPostID(int postid)
        {
            Posts posts = PostsService.GetPostsById(postid);
            string month = "BQ-M";
            if(posts!=null)
            {
                month = posts.CreateDate.Month.ToString();
               
            }
            return month;

        }

        public string getDayByPostID(int postid)
        {
            Posts posts = PostsService.GetPostsById(postid);
            string day = "BQ-D";
            if (posts != null)
            {
                day = posts.CreateDate.Day.ToString();

            }
            return day;

        }

        public string getPostCatagoryLink(int typeid)
        {
            var terms = TermsService.GetTerms(typeid);
            string catagorylink = ConfigHelper.SiteUrl + "index.aspx?" + "catagory=" + 0;
            string termsName = "默认分类";
            if (terms != null)
            {
                catagorylink = ConfigHelper.SiteUrl + "index.aspx?" + "catagory=" + terms.ID;
                termsName = terms.Name;
            }

            return string.Format("<a href=\"{0}\" title=\"分类:{1}\">{1}</a>", catagorylink, termsName);
            
        }

        public string getPostTagsLink(int tagsid)
        {
            var terms = TermsService.GetTerms(tagsid);
            string tagslink = ConfigHelper.SiteUrl + "index.aspx?" + "tags=" + 0;
            string termsName = "默认标签";
            if (terms != null)
            {
                tagslink = ConfigHelper.SiteUrl + "index.aspx?" + "tags=" + terms.ID;
                termsName = terms.Name;
            }

            return string.Format("<a href=\"{0}\" title=\"分类:{1}\">{1}</a>", tagslink, termsName);

        }
        #endregion

        #region comment
        /// <summary>
        /// 获取评论链接
        /// </summary>
        /// <param name="commentid"></param>
        /// <returns></returns>
        public string getCommentLink(int commentid)
        {
            var comment = CommentService.GetComment(commentid);
            string commentlink = ConfigHelper.SiteUrl + "post.aspx";
            if (comment != null)
            {
                //以后会有改动

                Posts post = PostsService.GetPostsById(comment.TargetID.Value);
                if(post!=null)
                {
                    commentlink = ConfigHelper.SiteUrl + "post.aspx?" + "id=" + post.ID;
                }
                
            }

            return commentlink;

        }
        #endregion

        #region string
        /// <summary>
        /// 剪切字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public string InterceptString(string  str,int len)
        {
            return StringHelper.CutString(StringHelper.RemoveHtml(str), len);
        }

        #endregion

    }//end BaseCode Class


 
    
}

